package com.imoblife.brainwavestus;

import com.imoblife.brainwavestus.adapter.LvBaseProductAdapter;
import com.imoblife.brainwavestus.customview.FreatureProductsView;
import com.imoblife.brainwavestus.customview.MenuList;
import com.imoblife.brainwavestus.listener.OnProductItemClickListener;
import com.imoblife.brainwavestus.model.Constant;
import com.imoblife.brainwavestus.obpurchaseSystem.OBpurchaseSystem;
import com.imoblife.brainwavestus.obpurchaseSystem.TusPointManager;
import com.imoblife.brainwavestus.pad.activity.NewShopActivity;
import com.imoblife.brainwavestus.pad.activity.ShopActivity;
import com.imoblife.brainwavestus.pad.activity.SpecialsProductActivity;
import com.imoblife.brainwavestus.product.Product;
import com.imoblife.brainwavestus.product.Store;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;

public class StoreActivity extends BaseActivity {
	
	private Context mCtx;
	private ListView mLvHotSale;
	private ArrayList<Product> mHotSaleProducts;
	private FreatureProductsView mFreatureProductsView;
	private Button newProductEnter;
	private Button allProductEnter;
	private Button specialProductEnter;
	private final String TAG = "StoreActivity";
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		mCtx = this;
		if(LaunchScreenActivity.IS_PAD) {
			
			//setContentView(R.layout.pad_store_main_layout);
			initTitleName(getString(R.string.store));
			initPadUi();
			initEnterState();
		}else {
			
			setContentView(R.layout.phone_store_main_layout);
			init();
		}		
	}

	protected void initEnterState() {
		newProductEnter = (Button) findViewById(R.id.new_product_enter);
		newProductEnter.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				startActivity(new Intent(StoreActivity.this,
						NewShopActivity.class));

			}
		});

		allProductEnter = (Button) findViewById(R.id.button_all_product);
		allProductEnter.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				startActivity(new Intent(StoreActivity.this,
						ShopActivity.class));

			}
		});
		specialProductEnter = (Button) findViewById(R.id.specials_product_enter);
		specialProductEnter.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				startActivity(new Intent(StoreActivity.this,
						SpecialsProductActivity.class));
			}
		});

	}
	
	private void initPadUi() {
		
		if (mFreatureProductsView == null) {
			mFreatureProductsView = new FreatureProductsView(this);
		}
		mFreatureProductsView
			.setContentView(R.layout.pad_store_main_layout);
		mFreatureProductsView.loading();
	}
	
	private void init() {
		
		initTitleName(getString(R.string.store));
		
		mLvHotSale = (ListView) findViewById(R.id.lv_hot_sale_product);
		
		Store store = getBrainWavesApplication().getStore();
		
		mHotSaleProducts = 
				store.getSaleGroupProducts(Constant.GROUP_HOT_SALE);
		
		LvBaseProductAdapter adapter 
			= new LvBaseProductAdapter(this, R.layout.item_common_lv_product_layout);
		
		adapter.setData(mHotSaleProducts)
				.setRightIconId(R.drawable.five_star)
				.setSaleGroupIcon(R.drawable.hot_icon);
		
		mLvHotSale.setAdapter(adapter);
		mLvHotSale.setOnItemClickListener(
				new OnProductItemClickListener(mCtx,
						mHotSaleProducts,
						Constant.SOURCE_FROM_STORE));
	}
	
	@Override
	protected void onStart() {		
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onDestroy() {		
		super.onDestroy();
	}
	
	public void onAllProductClicked(View v) {
		
		startActivity(new Intent(this,
				GroupAllProductCategoryActivity.class));
	}
	
	public void onDiscountProductClicked(View v) {
		
		Intent discountIntent = 
				new Intent(this,
						GroupCommonProductActivity.class);
		discountIntent.setAction(Constant.ACTION_SALE_GROUP_DISCOUNT);
		
		startActivity(discountIntent);
	}
	
	public void onNewProductClicked(View v) {
		
		Intent newIntent = 
				new Intent(this,
						GroupCommonProductActivity.class);
		newIntent.setAction(Constant.ACTION_SALE_GROUP_NEW);
		
		startActivity(newIntent);
	}
	
	@Override
	public void onTitleMenuClicked(View v) {
		
		MenuList menuList = new MenuList(v, this);

		menuList.addItem(
			getString(R.string.tus_credits) + 
				OBpurchaseSystem.getOBpurchaseSystem(this).getPoints(),
				new Runnable() {
					public void run() {
						OBpurchaseSystem.getOBpurchaseSystem(
								mCtx).showOffers();
					}
				});

		menuList.addItem(getString(R.string.update_shop),
				new Runnable() {
					public void run() {
						updateShop();
					}
				});

		menuList.addItem(getString(R.string.getCredits),
				new Runnable() {
					public void run() {
						new TusPointManager(mCtx);
					}
				});
		menuList.display();
	}
	
	public void updateShop() {

		
	}

}
